package com.dartsstats;

import java.util.ArrayList;
import java.util.List;

import com.dartsstats.datasource.GameDataSource;
import com.dartsstats.datasource.GameLineupDataSource;
import com.dartsstats.datasource.MatchDataSource;
import com.dartsstats.datasource.PlayerDataSource;
import com.dartsstats.datasource.TeamDataSource;

import android.content.Context;

public class GameState {
	
	public static final int TOP = 1;
	public static final int BOTTOM = 2;
	
	private int currentInning;
	private int topOrBottom;
	private Player currentHomeBatter;
	private Player currentAwayBatter;
	private Player runnerOnFirst;
	private Player runnerOnSecond;
	private Player runnerOnThird;
	private final long gameId;
	private final long matchId;
	private int homeScore;
	private int awayScore;
	private List<Integer> awayScores;
	private List<Integer> homeScores;
	private List<Player> awayPlayers;
	private List<Player> homePlayers;
	private Game game;
	private Match match;
	private Team homeTeam;
	private Team awayTeam;
	
	public GameState(long gameId, long matchId, Context context) {
		this.gameId = gameId;
		this.matchId = matchId;
		GameDataSource gameDataSource = new GameDataSource(context);
		game = gameDataSource.getGameByID((int)gameId);
		MatchDataSource matchDataSource = new MatchDataSource(context);
		match = matchDataSource.getMatchByID((int)matchId);
		TeamDataSource teamDataSource = new TeamDataSource(context);
		awayTeam = teamDataSource.getTeamByID(match.getAwayTeam());
		homeTeam = teamDataSource.getTeamByID(match.getHomeTeam());
		homeScore = 0;
		awayScore = 0;
		topOrBottom = TOP;
		currentInning = 1;
		awayScores = new ArrayList<Integer>();
		homeScores = new ArrayList<Integer>();
		GameLineupDataSource gameLineupDataSource = new GameLineupDataSource(context);
		List<GameLineup> gameLineupByGame = gameLineupDataSource.getGameLineupByGame(gameId);
		awayPlayers = new ArrayList<Player>();
		homePlayers = new ArrayList<Player>();
		populatePlayers(gameLineupByGame, context);
		currentHomeBatter = homePlayers.get(0);
		currentAwayBatter = awayPlayers.get(0);
	}
	
	private void populatePlayers(List<GameLineup> gameLineupByGame, Context context) {
		PlayerDataSource playerDataSource = new PlayerDataSource(context);
		for(GameLineup gameLineup : gameLineupByGame) {
			Player player = playerDataSource.getPlayerByID(gameLineup.getPlayer());
			if(player.getTeam() == awayTeam.getId()){
				awayPlayers.add(player);
			} else if(player.getTeam() == homeTeam.getId()) {
				homePlayers.add(player);
			} else {
				//ERROR CONDITION;
			}
		}
	}

	public Player getCurrentHomeBatter() {
		return currentHomeBatter;
	}

	public void setCurrentHomeBatter(Player currentHomeBatter) {
		this.currentHomeBatter = currentHomeBatter;
	}

	public Player getCurrentAwayBatter() {
		return currentAwayBatter;
	}

	public void setCurrentAwayBatter(Player currentAwayBatter) {
		this.currentAwayBatter = currentAwayBatter;
	}

	public int getCurrentInning() {
		return currentInning;
	}

	public void setCurrentInning(int currentInning) {
		this.currentInning = currentInning;
	}

	public int getTopOrBottom() {
		return topOrBottom;
	}

	public void setTopOrBottom(int topOrBottom) {
		this.topOrBottom = topOrBottom;
	}

	public int getHomeScore() {
		return homeScore;
	}

	public void setHomeScore(int homeScore) {
		this.homeScore = homeScore;
	}

	public int getAwayScore() {
		return awayScore;
	}

	public void setAwayScore(int awayScore) {
		this.awayScore = awayScore;
	}

	public List<Integer> getAwayScores() {
		return awayScores;
	}

	public void setAwayScores(List<Integer> awayScores) {
		this.awayScores = awayScores;
	}

	public List<Integer> getHomeScores() {
		return homeScores;
	}

	public void setHomeScores(List<Integer> homeScores) {
		this.homeScores = homeScores;
	}

	public static int getTop() {
		return TOP;
	}

	public static int getBottom() {
		return BOTTOM;
	}

	public long getGameId() {
		return gameId;
	}

	public long getMatchId() {
		return matchId;
	}

	public List<Player> getAwayPlayers() {
		return awayPlayers;
	}

	public void setAwayPlayers(List<Player> awayPlayers) {
		this.awayPlayers = awayPlayers;
	}

	public List<Player> getHomePlayers() {
		return homePlayers;
	}

	public void setHomePlayers(List<Player> homePlayers) {
		this.homePlayers = homePlayers;
	}

	public Game getGame() {
		return game;
	}

	public void setGame(Game game) {
		this.game = game;
	}

	public Match getMatch() {
		return match;
	}

	public void setMatch(Match match) {
		this.match = match;
	}

	public Team getHomeTeam() {
		return homeTeam;
	}

	public void setHomeTeam(Team homeTeam) {
		this.homeTeam = homeTeam;
	}

	public Team getAwayTeam() {
		return awayTeam;
	}

	public void setAwayTeam(Team awayTeam) {
		this.awayTeam = awayTeam;
	}
	
	public Player getRunnerOnFirst() {
		return runnerOnFirst;
	}

	public Player getRunnerOnSecond() {
		return runnerOnSecond;
	}

	public Player getRunnerOnThird() {
		return runnerOnThird;
	}
}
